﻿using ControlHelper.Attributes;
using Creator.Core.Component;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace DesktopCreator.Test
{
    /// <summary>
    /// TestWindow.xaml 的交互逻辑
    /// </summary>
    public partial class TestWindow : Window
    {
        public TestWindow()
        {
            InitializeComponent();

            Loaded += TestWindow_Loaded;
        }

        private void TestWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var cd = Creator.Utility.Reflection.GetClassDataTypes(typeof(IComponent));

            var paths = cd.Select(a => new { Path = TryGetPath(a), a.Name }).ToList();
            tv.SelectedItemChanged += Tv_SelectedItemChanged;
            foreach (var item in paths)
            {
                AddPathToTreeView(tv,item.Path,item.Name);  
            }

        }

        private void Tv_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (!(tv.SelectedItem is TreeViewItem item) || item.Tag == null) return;
     
            MessageBox.Show(item.Tag.ToString());

        }

        string TryGetPath(Type type)
        {
            string name = null;
            var cma = type.GetCustomAttribute<ComponentMenuAttribute>();
            if (cma != null)
            {
                name = cma.Path;
            }

            return name ?? type.Name;
        }

        private void AddPathToTreeView(TreeView treeView, string path, string name)
        {
            // 拆分路径
            string[] parts = path.Split('/');

            // 从根节点开始添加子节点
            TreeViewItem currentNode = null;
            foreach (string part in parts)
            {
                //查找相同目录下
                var existNode = treeView.Items.OfType<TreeViewItem>().FirstOrDefault(a => a.Header?.ToString() == part);
                if (treeView.Items.Count > 0 && existNode != null)
                {
                    currentNode = existNode;
                    continue;
                }

                // 如果当前节点为空，则创建根节点
                if (currentNode == null)
                {
                    currentNode = new TreeViewItem();
                    currentNode.Header = part;
                    treeView.Items.Add(currentNode);
                }
                else
                {
                    // 检查当前节点是否已经存在相同的子节点
                    bool nodeExists = false;
                    foreach (TreeViewItem item in currentNode.Items)
                    {
                        if (item.Header as string == part)
                        {
                            currentNode = item;
                            nodeExists = true;
                            break;
                        }
                    }

                    // 如果当前节点不存在相同的子节点，则创建新的子节点
                    if (!nodeExists)
                    {
                        TreeViewItem newNode = new TreeViewItem();
                        newNode.Header = part;
                        currentNode.Items.Add(newNode);
                        currentNode = newNode;
                    }
                }
            }
            currentNode.Tag = name;
        }
    }
}
